<template>
  <div class="login_page fillcontain">
    <transition name="form-fade" mode="in-out">
      <section class="form_contianer" v-show="showLogin">
        <div class="manage_tip">
          <p>默契考验后台管理系统</p>
        </div>
        <el-form :model="loginForm" :rules="rules" ref="loginForm">
          <el-form-item prop="username">
            <el-input v-model="loginForm.username" placeholder="请输入您的手机号" class="el_input"></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input v-model="loginForm.password" placeholder="请输入您的密码" type="password"   class="el_input"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="submitForm('loginForm')" class="submit_btn">登陆</el-button>
          </el-form-item>
        </el-form>
        <p class="tip">温馨提示：</p>
        <p class="tip">新用户请联系管理员开通</p>
        <p class="tip">老用户可凭账号密码登录</p>
      </section>
    </transition>
  </div>
</template>

<script>
import { login, getAdminInfo } from "@/api/getData";
import { mapActions, mapState, mapMutations } from "vuex";
import { setCookie } from '@/utils/storage'

export default {
  data() {
    return {
      loginForm: {
        username: "",
        password: ""
      },
      rules: {
        username: [
          { required: true, message: "用户名格式错误", trigger: "blur" }
        ],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }]
      },
      showLogin: false
    };
  },
  mounted() {
    this.showLogin = true;
    // if (!this.adminInfo.id) {
    //   this.getAdminData();
    // }
    // login({
    //   "mobile": "13121578640",
    //   "password": "123456"
    // })

  },
  computed: {
    ...mapState(["adminInfo"])
  },
  methods: {
    ...mapMutations(["saveAdminInfo"]),
    ...mapActions(["getAdminData"]),
    async submitForm(formName) {
      this.$refs[formName].validate(async valid => {
        if (valid) {
          try {
            const res = await login({
              mobile: this.loginForm.username,
              password: this.loginForm.password
            });
            if (res.code === 0) {
              // localStorage.setItem('token', res.data.token)
              setCookie('token', res.data.token)
              sessionStorage.setItem('adminInfo', JSON.stringify(res.data))
              this.saveAdminInfo(res.data)
              this.$message({
                type: "success",
                message: "登录成功"
              });
              this.$router.push("userList");
            } else {
              this.$message({
                type: "error",
                message: res.message
              });
            }
          } catch (err) {
            this.$message({
              type: "error",
              message: '登录错误，请重试'
            })
          }
        } else {
          this.$notify.error({
            title: "错误",
            message: "请输入正确的用户名密码",
            offset: 100
          });
          return false;
        }
      });
    }
  },
  watch: {
    adminInfo: function(newValue) {
      if (newValue.id) {
        this.$message({
          type: "success",
          message: "检测到您之前登录过，将自动登录"
        });
        this.$router.push("manage");
      }
    }
  }
};
</script>

<style lang="less" scoped>
@import "../style/mixin";
.login_page {
  width: 100%;
  height: 100%;
  background: url("https://shtimgcdn.youhaodongxi.com/f2e_assets/cms/assets/images/bg.jpg") no-repeat;
  background-size: cover
}
.manage_tip {
  position: absolute;
  width: 100%;
  top: -100px;
  left: 0;
  p {
    font-size: 34px;
    color: #fff;
  }
}
.form_contianer {
  .wh(400px, 300px);
  .ctp(400px, 300px);
  padding: 25px;
  border-radius: 5px;
  text-align: center;
  background-color: #fff;
  .submit_btn {
    width: 100%;
    font-size: 16px;
  }
}
.tip {
  font-size: 12px;
  color: red;
}
.form-fade-enter-active,
.form-fade-leave-active {
  transition: all 1s;
}
.form-fade-enter,
.form-fade-leave-active {
  transform: translate3d(0, -50px, 0);
  opacity: 0;
}
.el_input {
  margin-top: 15px;
}
</style>
